﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entities;
using Core;
using System.Data;

namespace DAL
{
    public class TaiKhoanDAL
    {
        public static List<TaiKhoan> LayTatCa()
        {
            try
            {
                return CBO.FillCollection<TaiKhoan>(DataProvider.Instance.ExecuteReader("aspnet_Users_LayTatCa"));

            }
            catch
            {

                return new List<TaiKhoan>();
            }
        }
        public static bool Them(TaiKhoan tk)
        {
            try
            {
                object rs = DataProvider.Instance.ExecuteNonQueryWithOutput("@IDTaiKhoan", "TaiKhoan_Them", tk.IDTaiKhoan, tk.TenTaiKhoan, tk.MatKhau, tk.HoTen, tk.Email, tk.GioiTinh, tk.NgaySinh, tk.TrangThai, tk.PhanQuen);
                return Convert.ToInt32(rs) > 0;
            }
            catch
            {

                return false;
            }
        }
        public static bool Sua(TaiKhoan tk)
        {
            try
            {
                int rs = DataProvider.Instance.ExecuteNonQuery("TaiKhoan_Sua", tk.IDTaiKhoan, tk.TenTaiKhoan, tk.MatKhau, tk.HoTen, tk.Email, tk.GioiTinh, tk.NgaySinh, tk.TrangThai, tk.PhanQuen);
                return Convert.ToInt32(rs) > 0;
            }
            catch
            {

                return false;
            }
        }
        public static bool Xoa(string Id)
        {
            try
            {
                int rs = DataProvider.Instance.ExecuteNonQuery("TaiKhoan_Xoa", Convert.ToInt32(Id));
                return Convert.ToInt32(rs) > 0;
            }
            catch
            {

                return false;
            }
        }


        public static TaiKhoan LayID(string Id)
        {
            try
            {
                return CBO.FillObject<TaiKhoan>(DataProvider.Instance.ExecuteReader("TaiKhoan_LayID", Convert.ToInt32(Id)));
            }
            catch
            {

                return null;
            }
        }
        public static bool DangKy(TaiKhoan tk)
        {

            try
            {
                object rs = DataProvider.Instance.ExecuteNonQueryWithOutput("@IDTaiKhoan", "TaiKhoan_DangKy",
                    tk.TenTaiKhoan, tk.MatKhau, tk.HoTen, tk.Email, tk.GioiTinh, tk.NgaySinh);
                return Convert.ToInt32(rs) > 0;
            }
            catch (Exception)
            {

                return false;
            }
        }
        public static List<TaiKhoan> Paging(string page, out int howManyPages)
        {
            IDataReader reader = null;
            try
            {
                int pageSize = GlobalConfiguration.PageSize;
                reader = DataProvider.Instance.ExecuteReader(
                     "TaiKhoan_Paging",
                     page,
                     GlobalConfiguration.PageSize);
                reader.Read();
                howManyPages = (int)Math.Ceiling((double)reader.GetInt32(0) /
        (double)pageSize);

                reader.NextResult();
                return CBO.FillCollection<TaiKhoan>(reader);
            }
            catch
            {
                if (reader != null && reader.IsClosed == false)
                    reader.Close();
                howManyPages = 0;
                return new List<TaiKhoan>();
            }
        }
        public static List<TaiKhoan> TimKiem(string tentaikhoan, string hoten)
        {

            try
            {
                return CBO.FillCollection<TaiKhoan>(DataProvider.Instance.ExecuteReader("TaiKhoan_TimKiem", tentaikhoan, hoten));
            }
            catch
            {

                return new List<TaiKhoan>();
            }

        }
        public static List<TaiKhoan> TimKiemHoTen(string hoten)
        {

            try
            {
                return CBO.FillCollection<TaiKhoan>(DataProvider.Instance.ExecuteReader("TaiKhoan_TimKiemHoTen", hoten));
            }
            catch
            {

                return new List<TaiKhoan>();
            }

        }
        public static List<TaiKhoan> TimKiemTenTK(string tentaikhoan)
        {

            try
            {
                return CBO.FillCollection<TaiKhoan>(DataProvider.Instance.ExecuteReader("TaiKhoan_TimKiemTenTK", tentaikhoan));
            }
            catch
            {

                return new List<TaiKhoan>();
            }

        }

    }

}
